﻿import cn.eminia.A2.utils.uMC;
import cn.eminia.A2.btnEffect.uBtn;
import flash.geom.Point;
import mx.utils.Delegate;
class cn.eminia.A2.io.Rat {
	// [置顶]
	// AS2.MoE.follow(this)
	function Rat() {
	}
	static function followMouseByXY2() 
	{
		
	}
	static function followMouse(scope:MovieClip,mc:MovieClip, obj:Object):Void {
		var ctrl = uMC.newMC();
		ctrl.mc = mc;
		ctrl.xo = mc._x -mc._parent._xmouse;
		ctrl.yo = mc._y -mc._parent._ymouse;
		ctrl.onPP =  (obj.onPP==null)?null: obj.onPP;
		ctrl.scope = scope;
		//
		mc.follow_ctrl = ctrl;
		//
		if (obj.onPP != null) {
			
			ctrl.onEnterFrame = function() {
				if(!_root.isOut){
					this.mc._x  = this.mc._parent._xmouse + this.xo;
					this.mc._y  = this.mc._parent._ymouse + this.yo;	
					Delegate.create(this.scope, this.onPP)( { mc:this.mc } );
				}
			};
		}else {
			ctrl.onEnterFrame = function() {
				if(!_root.isOut){
					this.mc._x  = this.mc._parent._xmouse + this.xo;
					this.mc._y  = this.mc._parent._ymouse + this.yo;		
				}
			};
		}
		
	}
	static function delfollow(mc:MovieClip, obj:Object):Void { 
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl)
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl.onEnterFrame)
		delete mc.follow_ctrl.onEnterFrame;
		removeMovieClip(mc.follow_ctrl);
		//_root['mpo'].trace(' -------------------------- ' ) 
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl)
		_root['mpo'].trace('监视 删除 控制器 '+mc.follow_ctrl.onEnterFrame)
	}
	static function followMouseWithOffset(mc:MovieClip ):Void 
	{ 
		mc.onEnterFrame = function() {
			this._x  = (this._parent._xmouse);
			this._y  = (this._parent._ymouse);
		};
	}
	static function followMouseWithTween(a:MovieClip, obj:Object):Void 
	{
		var d:String = obj.d;
		var limit:Array = obj.limit;
		var inside:Function = obj.inside;
		var outside:Function = obj.outside;
		//
		if (limit != null && limit.length != 2) {
			trace('驳回!! 请为 limit 正型');
			return;
		}
		if (d == null) {
			a.onEnterFrame = function() {
				this._x += (this._parent._xmouse-this._x)*.3;
				this._y += (this._parent._ymouse-this._y)*.3;
			};
			return;
		}
		a.d = d;
		if (limit == null) {
			a.onEnterFrame = function() {
				this[d] += (this._parent[d+'mouse']-this[d])*.3;
			};
			return;
		}
		// USAGE       
		//		AS2.EffoM.follow(girl_mc,{d:'_x', limit:[50, 404], inside:function () {
		//Mouse.hide();
		//}, outside:function () {
		//Mouse.show();
		//}});
		a.el = limit;
		a.ei = inside;
		a.eo = outside;
		a.es = null;
		a.onEnterFrame = function() {
			// KEEP FOR EVER !! 
			// var m = Math.max(el[0], Math.min(el[1], this._parent[d+'mouse']));
			var m = this._parent[d+'mouse'];
			var el = this.el;
			var ei = this.ei;
			var eo = this.eo;
			var es = this.es;
			this[d] += (Math.max(el[0], Math.min(el[1], this._parent[d+'mouse']))-this[d])*.3;
			if ((m<el[0] || m>el[1]) && (es == null || es == 'in')) {
				es = 'out';
				eo();
			} else if (es == null || es == 'out') {
				es = 'in';
				ei();
			}
		}; 
	}
	public static function isClick(scope:MovieClip,mc:MovieClip, vars:Object):Object {
		var obj:Object = (vars != null)?vars:{};  
		obj.onMouseDown = Delegate.create(obj, function () {
			if (mc.hitTest(_root._xmouse, _root._ymouse)) { 
				//trace("cn.eminia.A2.io.Rat::isClick = " )
				Delegate.create(scope,this.callback)({it:mc,id: uBtn.getLastID(mc._name)});
			}
		});
		Mouse.addListener(obj);
		return obj;
	}
	public static function onClick(scope:MovieClip,mc:MovieClip,_obj:Object):Object {
		return isClick(scope,mc,_obj)
	}
	public static function delClick(_obj:Object):Boolean {
		return delMouseCtrl(_obj);
	}
	public static function delMouseCtrl(_obj:Object):Boolean {
		Mouse.removeListener(_obj);
		_obj = null;
		delete _obj;
		return true;
	}
	public	static function delOver(mc) {
		
		Mouse.removeListener(mc.RatOver_obj);
		delete mc.RatOver_obj;
		mc.RatOver_obj = null;
	}
	public static function pauseOver(a:MovieClip, obj:Object) { 
		
	}
	public static function over(a:MovieClip, obj:Object) {
		if (obj == null) {
			var obj = {};
		}
		obj.a = a;
		obj.m = 0;
		obj.auto = (obj.auto == undefined) ? true : obj.auto;
		if (obj.scope != null) {
			var scope = obj.scope;
			obj.onIN = Delegate.create(scope, obj.onIN);
			obj.onOUT = Delegate.create(scope, obj.onOUT);
		}
		// KEEP FOR EVER !!        
		//a.onEnterFrame = Delegate.create(obj, function () {
		obj.onMouseMove = Delegate.create(obj, function () {
			if (a.hitTest(_root._xmouse, _root._ymouse,true) && (this.m == 0)) {
				if (this.auto) Mouse.hide(); 
				this.onIN({it:a});
				this.m = 1;
			} else if (!a.hitTest(_root._xmouse, _root._ymouse,true) && (this.m == 1)) {
				if (this.auto) Mouse.show();
				this.onOUT({it:a});
				this.m = 0;
			}
			updateAfterEvent();
		});
		/*
		var ctrl = MC.newMC();
		ctrl.onEnterFrame = Delegate.create(obj, function() {
			if (!a.hitTest(_root._xmouse, _root._ymouse) && (this.m == 1)) {
				if (this.auto) Mouse.show();
				this.onOUT({it:a});
				this.m = 0;
			}			
		})*/
		Mouse.addListener(obj);
		//a.EffoM_=obj;
		a.RatOver_obj=obj
		return obj;
	}
	static function equalMouseWithOffset(mc:MovieClip,offset:Point):Void 
	{
		var obj = { };
		mc.__equalMouseObj__ = obj;
		mc._x = mc._parent._xmouse + offset.x;		
		mc._y = mc._parent._ymouse + offset.y;		
		obj.onMouseMove = Delegate.create(mc, function () {
			mc._x = mc._parent._xmouse + offset.x;			
			mc._y = mc._parent._ymouse + offset.y;			
			updateAfterEvent();
		});
		Mouse.addListener(obj); 
	}
	static function equalMouse(mc:MovieClip):Void 
	{
		var obj = { };
		mc.__equalMouseObj__ = obj;
		mc._x = mc._parent._xmouse;
		mc._y = mc._parent._ymouse;
		obj.onMouseMove = Delegate.create(mc, function () {
			mc._x = mc._parent._xmouse;
			mc._y = mc._parent._ymouse;
			updateAfterEvent();
		});
		Mouse.addListener(obj); 
	}
	static function deleteEqualMouse(mc:MovieClip):Void 
	{
		Mouse.removeListener(mc.__equalMouseObj__);
	}
	static function del(a) {
		delete a.onEnterFrame;
	}
}
